/**
 * 全局访问路径
 */
var path = "http://127.0.0.1:8899/api/consumer/"
/**
 * 图片全局路径
 */
var imgs = "http://120.48.47.144:8899/images/";
/**
 * 页面全局路径
 */
var pathHtml = "http://127.0.0.1:8899/html/";

var load_leng = 1500; //指定高度加载一次
//当前页
var pageTotla=1;
var pageZiXunIndex = 1;
//总页数
var PageCount=0;

$(function() {
	/* 根据token获取用户信息 */
		get_user_mess_and_token();
		//自己代码  
		change_over();
		//查询所有资讯
		//informationAll();
		//查询阅读量前十的资讯
		tenAll();
		//懒加载模式
		//new_ziXun();
		//回到顶点
		backVertex();

});


//**************查询所有资讯**************************

function informationAll() {
	$.post(path + 'InformationController/my_information_all/' + pageZiXunIndex, function(data) {
		//$("#zixun2").remove();
		$("#tuijian").attr("class","jsx-1382330630 tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-text-common-black tw-text-18 tw-font-medium active tw-h-32");
		$("#show_hide").attr("class","jsx-1382330630 tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-h-32 tw-ml-28 tw-text-common-gray tw-text-16");
		//$("#zixun").css("visibility","visible");
		//$("#zixun2").css("visibility","hidden");
		var str = "";
		var str2 = "";
		$(data.ziXunList).each(function(index, item) {
			//阅读量
			var Readcount = item.information_read_count;
			//时间变量
			var information_release_date = "";
			var date1 = new Date(item.information_release_date); //开始时间
			var date2 = new Date(); //结束时间
			var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
			//alert(date3);
			var days = Math.floor(date3 / (24 * 3600 * 1000)); // 计算出相差天数
			//计算出小时数
			var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
			var hours = Math.floor(leave1 / (3600 * 1000));
			//计算出相差分钟数
			var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
			var minutes = Math.floor(leave2 / (60 * 1000));
			if (days != 0 && days <= 7) {
				information_release_date = days + "天前";
			} else if (days == 0 && hours != 0) {
				information_release_date = hours + "小时前";
			} else if (days == 0 && hours == 0 && minutes != 0) {
				information_release_date = minutes + "分钟前";
			} else {
				information_release_date = item.information_release_date;
			}
			//计算出相差秒数
			//    var leave3 = leave2 % (60*1000); //计算分钟数后剩余的毫秒数
			//    var seconds = Math.round(leave3 / 1000);
			// alert("相差"+days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒");
			/**
			 * 如果是四位数则直接用数字表示，
			 * 否则用万做单位
			 */
			// var information_read_count = item.information_read_count;
			// while(information_read_count >= 1){
			// 	information_read_count=information_read_count/10;
			// 	Readcount++;
			// }
			// if(Readcount>999){

			// }

			str += "<div class='common-card_wrapper__Inr_n'>" +
				"<div>" +
				"<div class='tw-relative tw-text-common-black' style='font-size: 0px;'><a " +
				" title='" + item.information_title + "'" +
				"' href='" + pathHtml + "car_information_details.html?" + item.information_id + "/" +
				item.car_model.car_series.car_series_id + "' target='_blank'" +
				"class='jsx-1095092229' rel='opener'>" +
				"<div class='tw-rounded-4 g-load-img-wrap'" +
				"style='display: block; overflow: hidden; position: relative; z-index: 0;'>" +
				"<div style='display: block; box-sizing: border-box; padding-top: 56%;'>" +
				"</div><img src='" + imgs + item.information_images.information_images_1 + "' " +
				" alt='" + item.information_title + "' width='250' height='140'" +
				"class='zoom'" +
				"style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
				"</div>" +
				"</a>" +
				"<div class='jsx-1904556275 author-avatar_avatar-wrap__1pWQN'>" +
				"<div class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'>" +
				"<a href='"+pathHtml+"car_my_homepage.html?"+item.information_release_user_fk_id+"' title='" + item.user.user_name +
				"个人主页 ' target='_blank' rel='opener'>" +
				"<div class='avatar tw-rounded-full' style='display: inline-block; max-width: 100%; overflow: hidden; position: relative; z-index: 0;'>" +
				"<div style='box-sizing: border-box; display: block; max-width: 100%;'>" +
				"<img alt='' src='data:image/svg+xml;utf-8,<svg width=&quot;40&quot; height=&quot;40&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>' aria-hidden='true' role='presentation' style='max-width: 100%; display: block; margin: 0px; border: none; padding: 0px;'>" +
				"</div><img src='" + imgs + item.user.user_head_portrait_url +
				"' " +
				"alt='" + item.user.user_name + "头像' width='40' height='40' class=''" +
				"style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
				"</div>" +
				"<i class='jsx-2217341492 avatar_author-v__2Xocb author-v v-yellow'></i>" +
				"</a></div>" +
				"<p title='" + item.user.user_name +
				"' class='jsx-1904556275 author-avatar_name__3ex3- name line-1'>" +
				"<a href='"+pathHtml+"car_my_homepage.html?"+item.information_release_user_fk_id+"' title='" + item.user.user_name +
				"个人主页 ' target='_blank' rel='opener'>" + item.user.user_name + "</a></p>" +
				"</div>" +
				"</div>" +
				"<h3 class='tw-leading-22 tw-text-16 tw-font-medium tw-text-common-black tw-text-justify line-2 g-active-link-text tw-mt-36'>" +
				"<a title='" + item.information_title + "' href='" + pathHtml +
				"car_information_details.html?" + item.information_id + "/" + item.car_model.car_series
				.car_series_id + "' target='_blank'" +
				"class='jsx-1095092229' rel='opener'>" + item.information_title + "</a>" +
				"</h3>" +
				"<p class='tw-mt-8 tw-h-20 tw-lead tw-text-common-gray tw-text-14'>" +
				"<a title=" + item.information_title + "' href='" + pathHtml +
				"car_information_details.html?" + item.information_id + "/" + item.car_model.car_series
				.car_series_id + "' target='_blank'" +
				"class='jsx-1095092229' rel='opener'>" +
				"<span class='tw-mr-8'>" + (item.information_read_count < 10000 ? item.information_read_count : (item.information_read_count / 10000).toFixed(2) + "万") + "次阅读</span>" +
				"<span>" + information_release_date + "</span></a></p>" +
				"</div>" +
				"</div>"

		});
		$("#zixun div").remove();
		$("#zixun").append(str);
		str2 += "<div class='pagination'>" +
			"<a href='#' id='pageShang' >&laquo;</a>";
		/* 分页 循环出所有的页数，并给当前页添加样式*/
		for (var i = 1; i <= data.Pages; i++) {
			if (pageZiXunIndex == i) {
				// 符合条件
				str2 += "<a class='active' href='#' pageNum='" + i + "' id='num'>" + i + "</a>";
			} else {
				str2 += "<a href='#' pageNum='" + i + "' id='num' >" + i + "</a>";
			}
		}
		str2 += "<a href='#' id='pageXia'>&raquo;</a>" +
			"</div>";
		$("#zixun").append(str2);
		PageCount = data.Pages;
	}, "json");
}

/****** 点击懒加载查询资讯 ********************/
function new_ziXun() {
	$("#zixun").remove();
	$("#show_hide").attr("class","jsx-1382330630 tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-text-common-black tw-text-18 tw-font-medium active tw-h-32");
	$("#show_hide").children().css("padding","30px");
	$("#tuijian").attr("class","jsx-1382330630 tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-h-32 tw-ml-28 tw-text-common-gray tw-text-16");
	$("#zixun").css("visibility","hidden");
	$.post(path + 'InformationController/my_information_all/' + pageTotla, function(data) {
		PageCount=data.Pages;   //总页数
		var str = "";
		$(data.ziXunList).each(function(index, item) {
			//阅读量
			var Readcount = item.information_read_count;
			//时间变量
			var information_release_date = "";
			var date1 = new Date(item.information_release_date); //开始时间
			var date2 = new Date(); //结束时间
			var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
			//alert(date3);
			var days = Math.floor(date3 / (24 * 3600 * 1000)); // 计算出相差天数
			//计算出小时数
			var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
			var hours = Math.floor(leave1 / (3600 * 1000));
			//计算出相差分钟数
			var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
			var minutes = Math.floor(leave2 / (60 * 1000));
			if (days != 0 && days <= 7) {
				information_release_date = days + "天前";
			} else if (days == 0 && hours != 0) {
				information_release_date = hours + "小时前";
			} else if (days == 0 && hours == 0 && minutes != 0) {
				information_release_date = minutes + "分钟前";
			} else {
				information_release_date = item.information_release_date;
			}
			str += "<div class='common-card_wrapper__Inr_n' style='width:300px;'>" +
				"<div>" +
				"<div class='tw-relative tw-text-common-black' style='font-size: 0px;'><a " +
				" title='" + item.information_title + "'" +
				"' href='" + pathHtml + "car_information_details.html?" + item.information_id + "/" +
				item.car_model.car_series.car_series_id+ "' target='_blank'" +
				"class='jsx-1095092229' rel='opener'>" +
				"<div class='tw-rounded-4 g-load-img-wrap'" +
				"style='display: block; overflow: hidden; position: relative; z-index: 0;'>" +
				"<div style='display: block; box-sizing: border-box; padding-top: 56%;'>" +
				"</div><img src='" + imgs + item.information_images.information_images_1 + "' " +
				" alt='" + item.information_title + "' width='250' height='140'" +
				"class='zoom'" +
				"style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
				"</div>" +
				"</a>" +
				"<div class='jsx-1904556275 author-avatar_avatar-wrap__1pWQN'>" +
				"<div class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'>" +
				"<a href='"+pathHtml+"car_my_homepage.html?"+item.information_release_user_fk_id+"' title='" + item.user.user_name +
				"个人主页 ' target='_blank' rel='opener'>" +
				"<div class='avatar tw-rounded-full' style='display: inline-block; max-width: 100%; overflow: hidden; position: relative; z-index: 0;'>" +
				"<div style='box-sizing: border-box; display: block; max-width: 100%;'>" +
				"<img alt='' src='data:image/svg+xml;utf-8,<svg width=&quot;40&quot; height=&quot;40&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>' aria-hidden='true' role='presentation' style='max-width: 100%; display: block; margin: 0px; border: none; padding: 0px;'>" +
				"</div><img src='" + imgs + item.user.user_head_portrait_url +
				"' " +
				"alt='" + item.user.user_name + "头像' width='40' height='40' class=''" +
				"style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
				"</div>" +
				"<i class='jsx-2217341492 avatar_author-v__2Xocb author-v v-yellow'></i>" +
				"</a></div>" +
				"<p title='" + item.user.user_name +
				"' class='jsx-1904556275 author-avatar_name__3ex3- name line-1'>" +
				"<a href='"+pathHtml+"car_my_homepage.html?"+item.information_release_user_fk_id+"' title='" + item.user.user_name +
				"个人主页 ' target='_blank' rel='opener'>" + item.user.user_name + "</a></p>" +
				"</div>" +
				"</div>" +
				"<h3 class='tw-leading-22 tw-text-16 tw-font-medium tw-text-common-black tw-text-justify line-2 g-active-link-text tw-mt-36'>" +
				"<a title='" + item.information_title + "' href='" + pathHtml +
				"car_information_details.html?" + item.information_id + "/" + item.car_model.car_series
				.car_series_id+ "' target='_blank'" +
				"class='jsx-1095092229' rel='opener'>" + item.information_title + "</a>" +
				"</h3>" +
				"<p class='tw-mt-8 tw-h-20 tw-lead tw-text-common-gray tw-text-14'>" +
				"<a title=" + item.information_title + "' href='" + pathHtml +
				"car_information_details.html?" + item.information_id + "/" + item.car_model.car_series
				.car_series_id+ "' target='_blank'" +
				"class='jsx-1095092229' rel='opener'>" +
				"<span class='tw-mr-8'>" + (item.information_read_count < 10000 ? item.information_read_count : (item.information_read_count / 10000).toFixed(2) + "万") + "次阅读</span>" +
				"<span>" + information_release_date + "</span></a></p>" +
				"</div>" +
				"</div>"
		
		});
		//$("#zixun2 div").remove();
		$("#zixun2").append(str);

	}, "json");

}

//**************判断时间**************************
/* 
 * 获得时间差,时间格式为 年-月-日 小时:分钟:秒 或者 年/月/日 小时：分钟：秒 
 * 其中，年月日为全格式，例如 ： 2010-10-12 01:00:00 
 * 返回精度为：秒，分，小时，天
 */

function GetDateDiff(startTime, endTime, diffType) {
	//将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
	startTime = startTime.replace(/\-/g, "/");
	endTime = endTime.replace(/\-/g, "/");

	//将计算间隔类性字符转换为小写
	diffType = diffType.toLowerCase();
	var sTime = new Date(startTime); //开始时间
	var eTime = new Date(endTime); //结束时间
	//作为除数的数字
	var divNum = 1;
	switch (diffType) {
		case "second":
			divNum = 1000;
			break;
		case "minute":
			divNum = 1000 * 60;
			break;
		case "hour":
			divNum = 1000 * 3600;
			break;
		case "day":
			divNum = 1000 * 3600 * 24;
			break;
		default:
			break;
	}
	return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}
//**************资讯分页**************************
$('#pageShang').live('click', function() {
	paging('b');
});
$('#pageXia').live('click', function() {
	paging('c');
});
$('#num').live('click', function() {
	pageZiXunIndex = $(this).attr('pageNum');
	informationAll();
});

function paging(type) {
	if (type == 'b') {
		//箭头上一页
		if (pageZiXunIndex > 1) {
			pageZiXunIndex--;
		} else {
			alert("已经是第一页了");
		}
		informationAll();
	} else if (type == 'c') {
		//箭头下一页
		if (pageZiXunIndex < PageCount) {
			pageZiXunIndex++;
		} else {
			alert("已经是末页了");
		}
		informationAll();
	}
}

//**************资讯跳转分页定位**************************


//**************查询阅读量前十的资讯**************************
function tenAll() {
	$.post(path + 'InformationController/tenInfoAll', function(data) {
		var str = "";
		$(data).each(function(index, item) {
			str += "<li class='jsx-4163308028 rank-item tw-relative rank-item_rank-item__2-fk-'>" +
				"<a href='" + pathHtml + "car_information_details.html?" + item.information_id + "/" +
				item.car_model.car_series.car_series_id +
				"' target='_blank' rel='nofollow' class='jsx-4163308028'>" +
				"<div class='jsx-4163308028 tw-flex tw-items-center tw-pl-40 tw-pr-8 tw-py-16 hover:tw-pr-6 tw-mx-4 tw-rounded-2 tw-border-1 tw-border-solid tw-border-transparent   hover:tw-border-common-yellow hover:tw-bg-common-yellow hover:tw-bg-opacity-12 hover:tw-font-medium'>" +
				"<p class='jsx-4163308028 tw-flex-auto line-1 tw-leading-24 tw-text-16'>" + item
				.information_title + "</p>" +
				"<i class='jsx-4163308028 icon tw-hidden tw-flex-none tw-w-16 tw-h-16'></i>" +
				"</div>" +
				"</a>" +
				"</li>"
		})
		$("#hotTen li").remove();
		$("#hotTen").append(str);

	}, "json");
}

function change_over(){
	if($("#zixun").css("visibility")=="visible"){
		$("#zixun").show();
		informationAll();  //分页查询所有资讯
	}else{
		$("#zixun2").show();
		new_ziXun();  //懒加载查询所有资讯
	}
}

/* 懒加载刷新页面 */
window.onscroll = function() {
	if($("#zixun").css("visibility")!="visible"){
		var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
		var win_top = window.innerHeight;
		if (scroll_top + win_top > load_leng && pageTotla != PageCount) {
			pageTotla++;
			new_ziXun();
			load_leng += 1200;
		}
	}
	
}

/* 回到顶点 */
function backVertex(windowTop){
	//向上移动到0px
	$("#gotoTop").click(function(){
		$('html,body').animate({scrollTop:'0px'},'slow');
	});
	
	//获取最小高度，无法入值则默认600
	windowTop?windowTop=windowTop:windowTop=600;
	
	//为窗口的scroll事件绑定函数
	$(window).scroll(function(){
		//获取当前高度 
		var height=$(window).scrollTop();
		if(height>windowTop){
			$("#gotoTop").fadeIn(500);//500毫秒速度
		}else{
			$("#gotoTop").fadeOut(500);
		}
	})
}



